import datetime

def first_day_of_month(date=None):
    if not date:
        date = datetime.datetime.now()
    if type(date) == str:
        date = datetime.datetime.strptime(date,'%Y-%m-%d')
    return (date.replace(day=1)).strftime('%Y-%m-%d')

def last_day_of_month(date=None):
    if not date:
        date = datetime.datetime.now()
    if date.month == 12:
        return date.replace(day=31).strftime('%Y-%m-%d')
    return (date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

def today():
    return datetime.datetime.now().strftime('%Y-%m-%d')

def tomorrow(today=None):
    if not today:
        today = datetime.datetime.now().strftime('%Y-%m-%d')
    today = datetime.datetime.strptime(today,'%Y-%m-%d')
    return (today + datetime.timedelta(days=1)).strftime('%Y-%m-%d')

def yesterday(today=None):
    if not today:
        today = datetime.datetime.now().strftime('%Y-%m-%d')
    today = datetime.datetime.strptime(today,'%Y-%m-%d')
    return (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

def parseDate(dateStr):
    dateRuzhang = None
    if dateStr:
        try:
            dateRuzhang = datetime.datetime.strptime(dateStr,'%Y-%m-%d %H:%M:%S')
        except:
            try:
                dateRuzhang = datetime.datetime.strptime(dateStr,'%Y-%m-%d %H:%M')
            except:
                try:
                    dateRuzhang = datetime.datetime.strptime('20%s' %dateStr,'%Y.%m.%d')
                except:
                    try:
                        dateRuzhang = datetime.datetime.strptime('%s' %dateStr,'%Y-%m-%d')
                    except:
                        try:
                            dateRuzhangInt = '%s' %int(dateStr)
                            dateRuzhang = datetime.datetime.strptime(dateRuzhangInt,'%Y%m%d')
                        except:
                            try:
                                dateRuzhang = datetime.datetime.strptime(dateRuzhangInt[:4]+'0'+dateRuzhangInt[4:],'%Y%m%d')
                            except:
                                try:
                                    __s_date = datetime.date(1899, 12, 31).toordinal() - 1
                                    dateRuzhang = datetime.date.fromordinal(__s_date + int(dateRuzhangInt))
                                except:
                                    pass
    return dateRuzhang